﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using WorkOrder.Model;
using WorkOrder.Business;
using System.Drawing;

namespace WorkOrder
{
    public partial class addException : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Model_Table_Exception model;
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windohref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.parent.location.href='../login.aspx';</script>");
            }
            if (Page.IsPostBack)
            {
                if (Request.Params["a"] == "0" && Request.Params["id"] != null && Request.Params["id"] != "")
                {
                    if (!new Business_Table_Permission().HasInsert(Session["UserID"].ToString(), "Table_Exception"))
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：您没有新增工单问题信息的权限！";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您没有新增工单问题信息的权限！');window.close();</script>");
                        return;
                    }
                    string tmp = Server.HtmlEncode(txtExcept.Value.Trim());
                    model = new Model_Table_Exception("0", seleExceptType.Value, Request.Params["id"], Server.HtmlEncode(txtExcept.Value.Trim()), DateTime.Now, Session["UserID"].ToString());
                    if (new Business_Table_Exception().Insert(model))
                    {
                        txtExcept.Value = "";
                        seleExceptType.SelectedIndex = seleExceptType.Items.Count - 1;
                        lblMsg.ForeColor = Color.DarkGreen;
                        lblMsg.Text = "提示信息：成功保存一条记录！";
                    }
                    else
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：保存数据错误，请检查您的输入！";
                    }
                }
            }
            else
            {
                seleExceptType.Items.Clear();
                seleExceptType.DataTextField = "exception_type";
                seleExceptType.DataValueField = "exception_type_id";
                seleExceptType.DataSource = new Business_Table_Exception().GetExceptType();
                seleExceptType.DataBind();
                seleExceptType.Items.Add(new ListItem("-- 请选择 --", "0"));
                seleExceptType.SelectedIndex = seleExceptType.Items.Count - 1;
            }
        }
    }
}
